﻿using Serilog.Core;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetMicro.Trace.Enricher
{
    public class ActivityTagEnricher : ILogEventEnricher
    {
        private const string ActivityTags = "Attributes";

        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            ArgumentNullException.ThrowIfNull(logEvent);

            var activity = Activity.Current;
            if (activity is not null)
            {
                var tags = activity.Tags.Select(tag => new LogEventProperty(tag.Key, new ScalarValue(tag.Value)));
                logEvent.AddPropertyIfAbsent(new LogEventProperty(ActivityTags, new StructureValue(tags)));
            }
        }
    }
}